package com.ruoyi.detect.service.Impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.detect.domain.Floor;
import com.ruoyi.detect.domain.Residential;
import com.ruoyi.detect.domain.User;
import com.ruoyi.detect.dto.MissonDto;
import com.ruoyi.detect.mapper.FloorMapper;
import com.ruoyi.detect.mapper.ResidentialMapper;
import com.ruoyi.detect.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.detect.mapper.MissionMapper;
import com.ruoyi.detect.domain.Mission;
import com.ruoyi.detect.service.IMissionService;

/**
 * 核酸任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-11
 */
@Service
public class MissionServiceImpl implements IMissionService {
    @Autowired
    private MissionMapper missionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ResidentialMapper residentialMapper;
    @Autowired
    private FloorMapper floorMapper;

    /**
     * 查询核酸任务
     *
     * @param id 核酸任务主键
     * @return 核酸任务
     */
    @Override
    public Mission selectMissionById(Long id) {
        return missionMapper.selectMissionById(id);
    }

    /**
     * 查询核酸任务列表
     *
     * @param mission 核酸任务
     * @return 核酸任务
     */
    @Override
    public List<Mission> selectMissionList(Mission mission) {
        return missionMapper.selectMissionList(mission);
    }

    /**
     * 新增核酸任务
     *
     * @param missonDto 核酸任务
     * @return 结果
     */
    @Override
    public int insertMission(MissonDto missonDto) {
        List<Long> residentialId = missonDto.getResidentialId();
        Long resId = residentialId.get(0);  // 小区id
        Long floorId = residentialId.get(1); // 楼id

        Mission mission = new Mission();
        mission.setEndtime(missonDto.getEndtime());  // 结束时间
        mission.setStarttime(missonDto.getStarttime()); // 开始时间
        mission.setLocation(missonDto.getLocation()); // 地点
        mission.setTaskname(missonDto.getTaskname()); // 任务描述
        mission.setResidentialId(resId);
        mission.setFoor_id(floorId);
        // 楼
        Floor floor = floorMapper.selectFloorById(floorId);
        // 判断
        Residential residential = residentialMapper.selectResidentialById(resId);
        residential.setState(1);
        residentialMapper.updateResidential(residential);
        floor.setState(1); // 检测中
        floorMapper.updateFloor(floor);
        List<User> list = userMapper.getList(floorId);// 总人数
        mission.setMumberoftasks(list.size());  // 设计总人数
        List<User> collect = list.stream().peek(user -> user.setStatus(2)).collect(Collectors.toList());
        collect.forEach(user -> {
            userMapper.updateUser(user);
        });
        return missionMapper.insertMission(mission);
    }

    /**
     * 修改核酸任务
     *
     * @param mission 核酸任务
     * @return 结果
     */
    @Override
    public int updateMission(Mission mission) {
        return missionMapper.updateMission(mission);
    }

    /**
     * 批量删除核酸任务
     *
     * @param ids 需要删除的核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteMissionByIds(Long[] ids) {
        return missionMapper.deleteMissionByIds(ids);
    }

    /**
     * 删除核酸任务信息
     *
     * @param id 核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteMissionById(Long id) {
        return missionMapper.deleteMissionById(id);
    }
}
